package com.yoshino.leetcode.oneHundred.eleventh;

class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        ListNode root = new ListNode(0), pre = root;
        root.next = head;
        while (pre.next != null) {
            if (pre.next.val == val) {
                pre.next = pre.next.next;
            } else {
                pre = pre.next;
            }
        }
        return root.next;
    }

    public ListNode getKthFromEnd(ListNode head, int k) {
        if (head == null) {
            return null;
        }
        int tot = 0;
        ListNode para = head;
        while (head != null) {
            tot++;
            head = head.next;
        }
        tot -= k;
        while (tot-- > 0) {
            para = para.next;
        }
        return para;
    }
}
